package za.ac.wits.elen7045.persistence.model;

import za.ac.wits.elen7045.persistence.PersistentObject;
import za.ac.wits.elen7045.persistence.encryption.Encryptor;
import za.ac.wits.elen7045.persistence.encryption.EncryptorFactory;

public class Credential extends PersistentObject {

	private Encryptor valueEncryptor = EncryptorFactory.getBasicEncryptor();
	private int ownerId;
	private String name;
	private String value;
	private boolean encrypted;
	
	public Credential() {
	}
	
	public Credential(String name) {
		this(name, null, false);
	}
	
	public Credential(String name, String value) {
		this(name, value, false);
	}
	
	public Credential(String name, String value, boolean encrypted) {
		this.name = name;
		this.encrypted = encrypted;
		setValue(value);
	}
	
	public int getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(int ownerId) {
		this.ownerId = ownerId;
	}

	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getValue() {
		return getValue(true);
	}
	public String getValue(boolean allowDecryption) {
		if (value != null && encrypted && allowDecryption) {
			return valueEncryptor.decrypt(value);
		}
		return value;
	}
	
	public void setValue(String value) {
		if (value != null && encrypted) {
			value = valueEncryptor.encrypt(value);
		}
		this.value = value;
	}
	
	public boolean isEncrypted() {
		return encrypted;
	}
	
	public void setEncrypted(boolean encrypted) {
		this.encrypted = encrypted;
	}
	
}
